/**
 * 
 */
package com.weiweiqin.controller.wechat;
import com.weiweiqin.constants.PropertyConfig;
import com.weiweiqin.model.Customer;
import com.weiweiqin.weixin.pojo.SNSUserInfo;
import com.weiweiqin.weixin.pojo.WeixinOauth2Token;
import com.weiweiqin.weixin.util.AdvancedUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.weiweiqin.service.CustomerService;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;

@Controller
@RequestMapping(value = "/loginAuth")



public class LoginAuthController {

	@Autowired
	private CustomerService customerService;

	private static Logger log = LoggerFactory.getLogger(LoginAuthController.class);

	private static String APPID = "";
	private static String SECRET = "";
	private static String URL = "";

	static {
		APPID = PropertyConfig.getInstance().getProperty("APPID");
		SECRET = PropertyConfig.getInstance().getProperty("SECRET");
		URL = PropertyConfig.getInstance().getProperty("URL");
	}

	/**
	 * 登录处理
	 *
	 * @param username
	 * @param
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping()
	public String fail(String username, Model model, HttpServletRequest request, ServletResponse response,HttpSession session) {

		String loginError = null;
		String errorCode = "";
		String code = request.getParameter("code");
		if (StringUtils.isNotEmpty(code)) {
			// 用户同意授权后，能获取到code
			// 用户同意授权
			log.info("code == " + code);
			if (!"authdeny".equals(code)) {
				// 获取网页授权access_token
				WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken(APPID, SECRET, code);
				// 网页授权接口访问凭证
				String accessToken = weixinOauth2Token.getAccessToken();
				// 用户标识
				String openId = weixinOauth2Token.getOpenId();
				SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId);

				log.info(">>>>>>>>>>>>>>>>>>>>>>>openId == " + openId);
				Customer cus = customerService.getByOpenid(openId);
				if(null == cus) {
					log.info(">>>>>>url"+snsUserInfo.getHeadImgUrl());
					log.info(">>>>>>nickname"+snsUserInfo.getNickname());
					cus = new Customer();
					cus.setAmount(new BigDecimal(0));
					cus.setIntegral(0);
					cus.setOpenid(openId);
					cus.setRegDate(new Date());
					cus.setHeadurl(snsUserInfo.getHeadImgUrl());
					cus.setNickname(snsUserInfo.getNickname());
					cus.setSex(snsUserInfo.getSex());
					cus.setCountry(snsUserInfo.getCountry());
					cus.setProvince(snsUserInfo.getProvince());
					cus.setCity(snsUserInfo.getCity());
					try {
						customerService.save(cus,"");
					}catch (Exception e) {
						log.info(e.getMessage());
					}

				}
				session.setAttribute("customer",cus);
				return "redirect:"+URL+"?vt"+new Date().getTime()+"#/index?auth=1";
			}
			model.addAttribute("username", username);
			model.addAttribute("loginError", loginError);
			model.addAttribute("errorCode", errorCode);
		}
		return "redirect:/fail";
	}
}
